package com.johnny.startup;

import java.util.ArrayList;
import java.util.List;

import com.johnny.connector.Connector;
import com.johnny.data.DataPool;
import com.johnny.data.Game28MathInfo;
import com.johnny.data.LevelDescriptionInfo;
import com.johnny.data.PlayerInfo;
import com.johnny.data.RoomInfo;
import com.johnny.data.TableInfo;
import com.johnny.data.TableState;
import com.johnny.gameLogic.GameLogic;
import com.johnny.util.Casino28GameMath;
import com.johnny.util.SerializableUtil;

public class Casino28App {
	public static Connector connector;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			initLevelDescriptionInfo();
			initRoomInfo();
			initPlayerInfo();
			
			GameLogic gameLogic=new GameLogic(2);
			gameLogic.start();

			connector = new Connector(8999, 2);
			connector.accept();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}

	}

	private static void initPlayerInfo() throws Exception {
		List<PlayerInfo> players = SerializableUtil.getInstance().getAllInfos(PlayerInfo.class);
		DataPool.getInstance().setPlayerInfos(players);
	}

	private static void initRoomInfo() throws Exception {
		List<RoomInfo> rooms = SerializableUtil.getInstance().getAllInfos(RoomInfo.class);
		if (rooms.size() == 0) {
			RoomInfo roomInfo = new RoomInfo();
			roomInfo.setFileName("0");
			roomInfo.setRoomId("0");
			roomInfo.setMaxBet(300000);
			roomInfo.setMinBet(3000);
			roomInfo.setChips(new long[] { 100, 1000, 10000, 100000 });
			roomInfo.setTables(createTables());
			roomInfo.setPlayers(new ArrayList<String>());
			SerializableUtil.getInstance().putInfo(roomInfo);
			rooms.add(roomInfo);

			roomInfo = new RoomInfo();
			roomInfo.setFileName("1");
			roomInfo.setRoomId("1");
			roomInfo.setMaxBet(3000000);
			roomInfo.setMinBet(30000);
			roomInfo.setChips(new long[] { 1000, 10000, 100000, 1000000 });
			roomInfo.setTables(createTables());
			roomInfo.setPlayers(new ArrayList<String>());
			SerializableUtil.getInstance().putInfo(roomInfo);
			rooms.add(roomInfo);

			roomInfo = new RoomInfo();
			roomInfo.setFileName("2");
			roomInfo.setRoomId("2");
			roomInfo.setMaxBet(30000000);
			roomInfo.setMinBet(300000);
			roomInfo.setChips(new long[] { 10000, 100000, 1000000, 10000000 });
			roomInfo.setTables(createTables());
			roomInfo.setPlayers(new ArrayList<String>());
			SerializableUtil.getInstance().putInfo(roomInfo);
			rooms.add(roomInfo);

			roomInfo = new RoomInfo();
			roomInfo.setFileName("3");
			roomInfo.setRoomId("3");
			roomInfo.setMaxBet(300000000);
			roomInfo.setMinBet(3000000);
			roomInfo.setChips(new long[] { 100000, 1000000, 10000000, 100000000 });
			roomInfo.setTables(createTables());
			roomInfo.setPlayers(new ArrayList<String>());
			SerializableUtil.getInstance().putInfo(roomInfo);
			rooms.add(roomInfo);

			roomInfo = new RoomInfo();
			roomInfo.setFileName("4");
			roomInfo.setRoomId("4");
			roomInfo.setMaxBet(2000000000);
			roomInfo.setMinBet(30000000);
			roomInfo.setChips(new long[] { 1000000, 10000000, 100000000, 1000000000 });
			roomInfo.setTables(createTables());
			roomInfo.setPlayers(new ArrayList<String>());
			SerializableUtil.getInstance().putInfo(roomInfo);
			rooms.add(roomInfo);
		}
		DataPool.getInstance().setRoomInfos(rooms);
	}

	private static List<TableInfo> createTables() {
		List<TableInfo> tables = new ArrayList<>();
		TableInfo table = new TableInfo();
		table.setTableId("0");
		table.setBanker("");
		table.setBankerCandidateList(new ArrayList<String>());
		table.setCloseTable(false);
		table.setTableState(TableState.waiting_banker);
		table.setDoor1Players(new ArrayList<String>());
		table.setDoor1Wagers(new ArrayList<Long>());
		table.setDoor2Players(new ArrayList<String>());
		table.setDoor2Wagers(new ArrayList<Long>());
		table.setDoor3Players(new ArrayList<String>());
		table.setDoor3Wagers(new ArrayList<Long>());
		table.setPlayer1("");
		table.setPlayer2("");
		table.setPlayer3("");
		table.setPlayer4("");
		table.setPlayer5("");
		table.setPlayer6("");
		table.setTimer(0);
		table.setDiceNumbers(new int[]{1,1});
		table.setCards(Casino28GameMath.getInstance().shuffle());
		table.setRemainingCardCount(table.getCards().size()-1);
		table.setMathInfo(new Game28MathInfo());
		tables.add(table);

		table = new TableInfo();
		table.setTableId("1");
		table.setBanker("");
		table.setBankerCandidateList(new ArrayList<String>());
		table.setCloseTable(false);
		table.setTableState(TableState.waiting_banker);
		table.setDoor1Players(new ArrayList<String>());
		table.setDoor1Wagers(new ArrayList<Long>());
		table.setDoor2Players(new ArrayList<String>());
		table.setDoor2Wagers(new ArrayList<Long>());
		table.setDoor3Players(new ArrayList<String>());
		table.setDoor3Wagers(new ArrayList<Long>());
		table.setPlayer1("");
		table.setPlayer2("");
		table.setPlayer3("");
		table.setPlayer4("");
		table.setPlayer5("");
		table.setPlayer6("");
		table.setTimer(0);
		table.setDiceNumbers(new int[]{1,1});
		table.setCards(Casino28GameMath.getInstance().shuffle());
		table.setRemainingCardCount(table.getCards().size()-1);
		table.setMathInfo(new Game28MathInfo());
		tables.add(table);

		table = new TableInfo();
		table.setTableId("2");
		table.setBanker("");
		table.setBankerCandidateList(new ArrayList<String>());
		table.setCloseTable(false);
		table.setTableState(TableState.waiting_banker);
		table.setDoor1Players(new ArrayList<String>());
		table.setDoor1Wagers(new ArrayList<Long>());
		table.setDoor2Players(new ArrayList<String>());
		table.setDoor2Wagers(new ArrayList<Long>());
		table.setDoor3Players(new ArrayList<String>());
		table.setDoor3Wagers(new ArrayList<Long>());
		table.setPlayer1("");
		table.setPlayer2("");
		table.setPlayer3("");
		table.setPlayer4("");
		table.setPlayer5("");
		table.setPlayer6("");
		table.setTimer(0);
		table.setDiceNumbers(new int[]{1,1});
		table.setCards(Casino28GameMath.getInstance().shuffle());
		table.setRemainingCardCount(table.getCards().size()-1);
		table.setMathInfo(new Game28MathInfo());
		tables.add(table);

		table = new TableInfo();
		table.setTableId("3");
		table.setBanker("");
		table.setBankerCandidateList(new ArrayList<String>());
		table.setCloseTable(false);
		table.setTableState(TableState.waiting_banker);
		table.setDoor1Players(new ArrayList<String>());
		table.setDoor1Wagers(new ArrayList<Long>());
		table.setDoor2Players(new ArrayList<String>());
		table.setDoor2Wagers(new ArrayList<Long>());
		table.setDoor3Players(new ArrayList<String>());
		table.setDoor3Wagers(new ArrayList<Long>());
		table.setPlayer1("");
		table.setPlayer2("");
		table.setPlayer3("");
		table.setPlayer4("");
		table.setPlayer5("");
		table.setPlayer6("");
		table.setTimer(0);
		table.setDiceNumbers(new int[]{1,1});
		table.setCards(Casino28GameMath.getInstance().shuffle());
		table.setRemainingCardCount(table.getCards().size()-1);
		table.setMathInfo(new Game28MathInfo());
		tables.add(table);

		return tables;
	}

	private static void initLevelDescriptionInfo() throws Exception {
		List<LevelDescriptionInfo> levels = SerializableUtil.getInstance().getAllInfos(LevelDescriptionInfo.class);
		if (levels.size() == 0) {
			LevelDescriptionInfo levelDescriptionInfo = new LevelDescriptionInfo();
			levelDescriptionInfo.setFileName("1");
			levelDescriptionInfo.setLevel("1");
			levelDescriptionInfo.setLevelName("Level 1");
			levelDescriptionInfo.setMaxEmpiricalValue(100);
			SerializableUtil.getInstance().putInfo(levelDescriptionInfo);
			levels.add(levelDescriptionInfo);

			levelDescriptionInfo = new LevelDescriptionInfo();
			levelDescriptionInfo.setFileName("2");
			levelDescriptionInfo.setLevel("2");
			levelDescriptionInfo.setLevelName("Level 2");
			levelDescriptionInfo.setMaxEmpiricalValue(1000);
			SerializableUtil.getInstance().putInfo(levelDescriptionInfo);
			levels.add(levelDescriptionInfo);

			levelDescriptionInfo = new LevelDescriptionInfo();
			levelDescriptionInfo.setFileName("3");
			levelDescriptionInfo.setLevel("3");
			levelDescriptionInfo.setLevelName("Level 3");
			levelDescriptionInfo.setMaxEmpiricalValue(10000);
			SerializableUtil.getInstance().putInfo(levelDescriptionInfo);
			levels.add(levelDescriptionInfo);

			levelDescriptionInfo = new LevelDescriptionInfo();
			levelDescriptionInfo.setFileName("4");
			levelDescriptionInfo.setLevel("4");
			levelDescriptionInfo.setLevelName("Level 4");
			levelDescriptionInfo.setMaxEmpiricalValue(100000);
			SerializableUtil.getInstance().putInfo(levelDescriptionInfo);
			levels.add(levelDescriptionInfo);

			levelDescriptionInfo = new LevelDescriptionInfo();
			levelDescriptionInfo.setFileName("5");
			levelDescriptionInfo.setLevel("5");
			levelDescriptionInfo.setLevelName("Level 5");
			levelDescriptionInfo.setMaxEmpiricalValue(1000000);
			SerializableUtil.getInstance().putInfo(levelDescriptionInfo);
			levels.add(levelDescriptionInfo);
		}
		DataPool.getInstance().setLevelDescriptionInfos(levels);
	}

}
